package day4.liberary_myslef;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: watermelon
 * Date: 2025-05-30
 * Time: 11:06
 */
class book{
    String Auther;
    int Year;
    String name;
    book(String Auther,int Year,String name)
    {
        this.Auther=Auther;
        this.Year=Year;
        this.name=name;
    }
    public String toString() {
        return "书名:"+name+"作者:"+Auther+"出版年份:"+Year;
    }
}

public class Liberary {
    static book[]Allbook=new book[100];
    static book nowbook=null;
    static int capacity=0;
    public static void Attendant()
    {
        int count=0;
        do {
            System.out.println("******管理员菜单******");
            System.out.println("1.查找图书");
            System.out.println("2.新增图书");
            System.out.println("3.删除图书");
            System.out.println("4.显示图书");
            System.out.println("0.退出系统");
            System.out.print("请输入要进行的操作:");
            Scanner scanner = new Scanner(System.in);
            count = scanner.nextInt();
            if(count==1)
            {
                System.out.println("请输入该图书的名称");
                String bookname=scanner.next();
                for (int i = 0; i < Allbook.length; i++) {
                    if(Allbook[i]==null)
                    {
                        System.out.println("查找失败,没有该图书");
                        break;
                    } else if (Allbook[i].name.equals(bookname)) {
                        System.out.println("查找成功索引为:"+i);
                        break;
                    }
                }
            } else if (count==2) {
                if(capacity==100)
                {
                    System.out.println("图书已经满了");
                    continue;
                }
                System.out.println("请输入新增图书的名称");
                String newbookname=scanner.next();
                System.out.println("请输入新增作者的名称");
                String newbookauthor=scanner.next();
                System.out.println("请输入图书发行的年份");
                int newbookyear=scanner.nextInt();
                book newbook=new book(newbookauthor,newbookyear,newbookname);
                Allbook[capacity]=newbook;
                capacity++;
            } else if (count==3) {
                if(capacity==0)
                {
                    System.out.println("没有图书可以删除");
                    continue;
                }
                System.out.println("请输入删除图书的引索:");
                int idex=scanner.nextInt();
                for (int i = idex; i < capacity; i++) {
                    Allbook[i]=Allbook[i+1];
                }
                capacity--;
            } else if (count==4) {
                for (int i = 0; i < capacity; i++) {
                    System.out.println(i+Allbook[i].name+Allbook[i].Auther+Allbook[i].Year);
                }
            }else if(count==0) {
                System.out.println("成功退出");
            }
            else {
                System.out.println("操作错误，请重新输入");
            }
        }while(count!=0);
    }
    public static void Comsumer()
    {
        int count=0;
        do {
            System.out.println("******管理员菜单******");
            System.out.println("1.查找图书");
            System.out.println("2.借阅图书");
            System.out.println("3.归还图书");
            System.out.println("0.退出系统");
            System.out.print("请输入要进行的操作:");
            Scanner scanner = new Scanner(System.in);
            count = scanner.nextInt();
            if(count==1)
            {
                System.out.println("请输入该图书的名称:");
                String bookname=scanner.next();
                for (int i = 0; i < Allbook.length; i++) {
                    if(Allbook[i]==null)
                    {
                        System.out.println("查找失败,没有该图书");
                        break;
                    } else if (Allbook[i].name.equals(bookname)) {
                        System.out.println("查找成功索引为:"+i);
                        break;
                    }
                }
            } else if (count==2) {
                System.out.println("请输入被借图书的索引:");
                int index=scanner.nextInt();
                nowbook=Allbook[index];
                for(int i=index;i<capacity;i++)
                {
                    Allbook[i]=Allbook[i+1];
                }
                capacity--;
            }
            else if(count==3)
            {
                if(nowbook==null)
                {
                    System.out.println("归还失败,没有可还图书");
                }
                else {
                    Allbook[capacity]=nowbook;
                    nowbook=null;
                    capacity++;
                    System.out.println("归还图书成功");
                }
            }
            else if(count==0)
            {
                System.out.println("退出成功");
            }
            else
            {
                System.out.println("输入错误请重新输入");
            }
        }while(count!=0);
    }
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int id;
        System.out.print("请输入你的名字:");
        String name=scanner.next();
        do {
            System.out.println("1.管理员,2.用户,0.退出系统");
            System.out.print("请输入你的身份:");
            id = scanner.nextInt();
            if (id == 1) {
                Attendant();
            } else if (id == 2) {
                Comsumer();
            } else if (id == 0) {
                System.out.println("退出系统成功");
            } else {
                System.out.println("输入错误,请重新输入");
            }
        }while(id!=0);

    }
}
